﻿using System;
using Protocol.Code;
using Protocol.Constant;
using Protocol.Dto;
using Script.Audio;
using Script.Framework;
using Script.UI;
using Script.UI.Msg;

namespace Script.Net.Impl
{
    public class ChatHandler : HandlerBase
    {
        // 变量声明
        private readonly ChatMsg chatMsg = new ChatMsg();


        public override void OnReceive(int subCode, object value)
        {
            switch (subCode)
            {
                case ChatCode.SRES:
                {
                    if (value is ChatDto dto)
                    {
                        int userId = dto.UserId;
                        int chatType = dto.ChatType;

                        string text = Constant.GetChatString(chatType);

                        chatMsg.Change(userId, chatType, text);


                        // 显示文字
                        Dispatch(AreaCode.UI, UIEvent.PLAYER_CHAT, chatMsg);

                        // 播放声音
                        Dispatch(AreaCode.AUDIO, AudioEvent.PLAY_EFFECT_AUDIO, "Chat/Chat_" + chatType);
                    }

                    break;
                }
                default:
                    throw new Exception("Something Wrong");
            }
        }
    }
}